package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("addressBook")
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址相关接口")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;
    @PostMapping
    @ApiOperation("添加地址")
    public Result add(@RequestBody AddressBook addressBook){

        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(1);
        System.out.println(addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("获取地址列表")
    public Result<List<AddressBook>> list(){
        return Result.success(addressBookService.list());
    }
    @GetMapping("/default")
    @ApiOperation("获取默认地址")
    public Result<AddressBook> getDefault(){
        return Result.success(addressBookService.getDefault());
    }
    @PutMapping
    @ApiOperation("更新地址")
    public Result update(@RequestBody AddressBook addressBook){
        System.out.println(addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }
    @DeleteMapping
    @ApiOperation("删除地址")
    public Result delete(Long id){
        addressBookService.delete(id);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("获取地址")
    public Result<AddressBook> get(@PathVariable Long id){
        return Result.success(addressBookService.get(id));
    }
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(Integer id){
        addressBookService.setDefault(id);
        return Result.success();
    }
}
